package com.excesys.exsecs.linkage.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.linkage.entity.SceneLinkage;
import com.excesys.exsecs.linkage.service.OperateLogsService;
import com.excesys.exsecs.linkage.service.SceneLinkageService;
import com.excesys.exsecs.linkage.vo.SceneLinkageVO;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 场景联动
 *
 * @author zhangtailong
 * @date 2024-05-20 14:09:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sceneLinkage")
@Tag(description = "sceneLinkage", name = "场景联动管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SceneLinkageController {

    private final SceneLinkageService sceneLinkageService;
    private final OperateLogsService operateLogsService;

    /**
     * 分页查询
     *
     * @param page         分页对象
     * @param sceneLinkage 场景联动
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_view')")
    public R getSceneLinkagePage(@ParameterObject Page page, @ParameterObject SceneLinkage sceneLinkage) {
        LambdaQueryWrapper<SceneLinkage> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(sceneLinkage.getSceneName()),
						SceneLinkage::getSceneName, sceneLinkage.getSceneName())
				.eq(StringUtils.isNotBlank(sceneLinkage.getActionType()),
						SceneLinkage::getActionType,sceneLinkage.getActionType())
				.orderByDesc(SceneLinkage::getId);

        return R.ok(sceneLinkageService.page(page, wrapper));
    }


    /**
     * 通过 linkageId 查询场景联动
     *
     * @param linkageId linkageId
     * @return R
     */
    @Operation(summary = "通过linkageId查询", description = "通过linkageId查询")
    @GetMapping("/{linkageId}")
    @PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_view')")
    public R getById(@PathVariable("linkageId") String linkageId) {
        return R.ok(sceneLinkageService.getByLinkageId(linkageId), "查询成功");
    }


    /**
     * 新增场景联动
     *
     * @param sceneLinkageVO 场景联动
     * @return R
     */
    @Operation(summary = "新增场景联动", description = "新增场景联动")
    @SysLog("新增场景联动")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_add')")
    public R save(@RequestBody SceneLinkageVO sceneLinkageVO) {
        Map map = sceneLinkageService.saveOrUpdate(sceneLinkageVO);
        return R.ok(map.get("msg"));
    }

    /**
     * 修改场景联动
     *
     * @param sceneLinkage 场景联动
     * @return R
     */
    @Operation(summary = "修改场景联动", description = "修改场景联动")
    @SysLog("修改场景联动")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_edit')")
    public R updateById(@RequestBody SceneLinkage sceneLinkage) {
        return R.ok(sceneLinkageService.updateById(sceneLinkage));
    }

    /**
     * 通过id删除场景联动
     *
     * @param linkageId linkageId列表
     * @return R
     */
    @Operation(summary = "通过id删除场景联动", description = "通过id删除场景联动")
    @SysLog("通过id删除场景联动")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_del')")
    public R removeById(@RequestBody String linkageId) {
        return R.ok(sceneLinkageService.removeBatchByLinkageId(linkageId), "删除成功");
    }

    /*
     *操作 - 启动状态 运行状态 （1启动  2 停止 ）
     * */
    @Operation(summary = "运行状态 （1启动  2 停止 ）", description = "运行状态 （1启动  2 停止 ）")
    @GetMapping("/updateStatus")
    @PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_edit')")
    public R updateStatus(String linkageId, Integer status) {
        return R.ok(sceneLinkageService.updateStatus(linkageId, status), "操作成功");
    }

    /*
     *操作 - 日志
     * */
    @Operation(summary = "日志", description = "日志")
    @GetMapping("/getLog")
    @PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_edit')")
    public R getLog(@ParameterObject Page page,String linkageId) {
        return R.ok(sceneLinkageService.getLog(page,linkageId), "操作成功");
    }

    /**
     * 导出excel 表格
     * @param sceneLinkage 查询条件
     * @param ids          导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_export')")
    public List<SceneLinkage> export(SceneLinkage sceneLinkage, Integer[] ids) {
        return sceneLinkageService.list(Wrappers.lambdaQuery(sceneLinkage).in(ArrayUtil.isNotEmpty(ids), SceneLinkage::getId, ids));
    }

	/**
	 * 通过 linkageId 查询场景联动
	 *
	 * @param linkageId linkageId
	 * @return R
	 */
	@Operation(summary = "通过linkageId触发规则", description = "通过linkageId触发规则")
	@GetMapping("/operate/{linkageId}")
	@PreAuthorize("@pms.hasPermission('exsecs_sceneLinkage_view')")
	public R operate(@PathVariable("linkageId") String linkageId) throws Exception {
		return R.ok(sceneLinkageService.operate(linkageId), "执行成功");
	}
}